<script setup lang="ts">
import { ref, onMounted } from 'vue'
import doctorCard from './doctorCard.vue'
import { useWindowSize } from '@vueuse/core'
import { getDoctorListApi } from '@/services/consult'
import type { DoctorList, DoctorPage, PageParams } from '@/types/consult'
import { showToast } from 'vant'

const { width } = useWindowSize() //推荐关注医生做响应式

const list = ref<DoctorList>([])
const params = ref<PageParams>({
  current: 1,
  pageSize: 5
})
const pageTotal = ref<number>(0)
const getDoctorList = async () => {
  const res = await getDoctorListApi(params.value)
  list.value.push(...res.data.rows)
  pageTotal.value = res.data.pageTotal
}
const onChange = async () => {
  // 滑动的时候让当前页++ 当前页等于总页数时停止
  if (params.value.current >= pageTotal.value) {
    return showToast('没有更多数据了')
  } else {
    params.value.current++
    getDoctorList()
  }
}
onMounted(() => {
  getDoctorList()
})
</script>

<template>
  <div class="follow-doctor">
    <div className="head">
      <p>推荐关注</p>
      <a href="javascript:;"> 查看更多<i class="van-icon van-icon-arrow" /></a>
    </div>
    <div class="body">
      <!-- swipe 组件 -->
      <van-swipe
        :width="(150 / 375) * width"
        :showIndicators="false"
        :loop="false"
        @drag-start="onChange"
      >
        <van-swipe-item v-for="item in list" :key="item.id">
          <doctorCard :item="item"></doctorCard>
        </van-swipe-item>
      </van-swipe>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.follow-doctor {
  background-color: var(--cp-bg);
  height: 250px;
  .head {
    display: flex;
    justify-content: space-between;
    height: 45px;
    align-items: center;
    padding: 0 15px;
    font-size: 13px;
    > a {
      color: var(--cp-tip);
    }
  }
  .body {
    width: 100%;
    overflow: hidden;
  }
}
</style>
